"""
@author: yc_scorpio
@time: 2021/4/14 23:55
@software: pycharm
@file: 2021XXXXLMP_XX.csv
@function: draw curves
"""
import matplotlib.pyplot as plt
import csv
# 读取目标表格文件
from numpy import linspace

Fname = "C:/Users/jsy12/Desktop/20211130LMP_0.05_citric15%_2.csv"

# 定义数组化函数
def array_x(column):
    ## 读指定列数据
    with open(Fname) as f:
        reader = csv.reader(f)
        tmp = [row[column] for row in reader]
    del tmp[0]
    tmp = [x for x in tmp if x != '']
    tmp = [float(x) for x in tmp]
    return tmp

# 定义数组
V1 = array_x(0)
C1 = array_x(1)
V2 = array_x(2)
C2 = array_x(3)
V22 = []
c1_max = str(max(C1, key=abs))
c2_max = str(max(C2, key=abs))

efficiency = max(C2, key=abs)/max(C1, key=abs)  # 求解首圈效率
efficiency = round(efficiency, 4)  # 取小数点后四位
efficiency = format(efficiency, '.2%')  # 取百分号后两位
text_box = "Window   = 2.5~4.5V\nCurrent   = 8.5mA/g\nEfficiency= " + str(efficiency)  # 生成文本内容

# 画图设置
plt.plot(C1, V1, 'maroon', label="Charge")  # 充电曲线
plt.plot(C2, V2, 'maroon', label="Discharge")  # 放电曲线

plt.vlines(max(C1, key=abs), 2.1, 4.55, colors='r', linestyles='-.', lw=1)
plt.vlines(max(C2, key=abs), 2.1, 4.55, colors='r', linestyles='-.', lw=1)
# plt.legend(loc='best')
plt.text(20, 2.5, text_box, fontdict={'size': 12, 'color': 'k'}, bbox=dict(facecolor="maroon", alpha=.3))
plt.text(max(C1, key=abs)-9, 3.7, c1_max, fontdict={'size': 12, 'color': 'k'})
plt.text(max(C2, key=abs)-15, 3.7, c2_max, fontdict={'size': 12, 'color': 'k'})
plt.title('Charge/Discharge curve of first cycle', fontsize=15)
plt.xlabel('Specific Capacity/(mAh·g$^{-1}$)', fontsize=14)
plt.ylabel('Potential/(V vs. Li/Li$^+$)', fontsize=14)
plt.savefig("C:/Users/jsy12/Desktop/20211130LMP_0.05_citric15%_2.png")
plt.show()